package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.logi.sys.bo.LogiCarrierQueryBo;
import com.piggy.logi.sys.bo.LogiProjectCarrierEditBo;
import com.piggy.logi.sys.bo.LogiProjectCarrierQueryBo;
import com.piggy.logi.sys.domain.LogiProjectCarrier;
import com.piggy.logi.sys.mapper.LogiProjectCarrierMapper;
import com.piggy.logi.sys.service.ILogiCarrierService;
import com.piggy.logi.sys.service.ILogiProjectCarrierService;
import com.piggy.logi.sys.vo.LogiCarrierExtVo;
import com.piggy.logi.sys.vo.LogiProjectCarrierExtVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目承运商管理Service业务层处理
 *
 * @author piggy
 * @date 2023-08-14
 */
@Slf4j
@Service
public class LogiProjectCarrierServiceImpl extends ServiceImpl<LogiProjectCarrierMapper, LogiProjectCarrier> implements ILogiProjectCarrierService {

    @Resource
    private ILogiCarrierService carrierService;

    @Override
    public LogiProjectCarrierExtVo queryById(Long id){
        LogiProjectCarrierExtVo vo = getVoById(id, LogiProjectCarrierExtVo.class);
        if (ObjectUtil.isEmpty(vo)) {
            return vo;
        }

        return fillData(Lists.newArrayList(vo)).get(0);
    }

    @Override
    public TableDataInfo<LogiProjectCarrierExtVo> queryPageList(LogiProjectCarrierQueryBo bo) {
        PagePlus<LogiProjectCarrier, LogiProjectCarrierExtVo> result = pageVo(PageUtils.buildPagePlus(bo, true), buildQueryWrapper(bo), LogiProjectCarrierExtVo.class);
        result.setRecordsVo(fillData(result.getRecordsVo()));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiProjectCarrierExtVo> queryList(LogiProjectCarrierQueryBo bo) {
        return fillData(listVo(buildQueryWrapper(bo), LogiProjectCarrierExtVo.class));
    }

    public List<LogiProjectCarrierExtVo> fillData(List<LogiProjectCarrierExtVo> extVos) {

        List<Long> carriarIds = extVos.stream().map(LogiProjectCarrierExtVo::getCarrierId).distinct().collect(Collectors.toList());
        carriarIds.add(-1L);
        List<LogiCarrierExtVo> carrierList = carrierService.queryList(new LogiCarrierQueryBo().setIdList(carriarIds));
        LinkedHashMap<Long, LogiCarrierExtVo> carrierMap = carrierList.stream().collect(LinkedHashMap::new, (m, v) -> m.put(v.getCarrierId(), v), LinkedHashMap::putAll);

        extVos = extVos.stream().peek(Any-> {
            Any.setCarrier(carrierMap.get(Any.getCarrierId()));
        }).collect(Collectors.toList());

        return extVos;
    }

    private LambdaQueryWrapper<LogiProjectCarrier> buildQueryWrapper(LogiProjectCarrierQueryBo bo) {
        LambdaQueryWrapper<LogiProjectCarrier> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, LogiProjectCarrier::getId, bo.getId());
        lqw.eq(bo.getProjectId() != null, LogiProjectCarrier::getProjectId, bo.getProjectId());
        lqw.eq(bo.getCarrierId() != null, LogiProjectCarrier::getCarrierId, bo.getCarrierId());
        lqw.in(CollUtil.isNotEmpty(bo.getProjectIdList()), LogiProjectCarrier::getProjectId, bo.getProjectIdList());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(LogiProjectCarrierEditBo bo) {
        LogiProjectCarrier add = BeanUtil.toBean(bo, LogiProjectCarrier.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(LogiProjectCarrierEditBo bo) {
        LogiProjectCarrier update = BeanUtil.toBean(bo, LogiProjectCarrier.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchByAddBo(List<LogiProjectCarrierEditBo> boList) {
        if (CollUtil.isEmpty(boList)) {
            return false;
        }
        List<LogiProjectCarrier> bookList = BeanUtil.copyToList(boList, LogiProjectCarrier.class);
        return this.saveBatch(bookList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchByUpdateBo(List<LogiProjectCarrierEditBo> boList) {
        if (CollUtil.isEmpty(boList)) {
            return false;
        }

        this.remove(Wrappers.<LogiProjectCarrier>lambdaQuery().eq(LogiProjectCarrier::getProjectId, boList.get(0).getProjectId()));
        return this.saveOrUpdateBatch(BeanUtil.copyToList(boList, LogiProjectCarrier.class));
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(LogiProjectCarrier entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    @Transactional
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
